package com.zlp.mall.portal.modules.pms.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zlp.mall.common.util.BeanToUtils;
import com.zlp.mall.portal.modules.pms.dto.resp.*;
import com.zlp.mall.portal.modules.pms.model.Product;
import com.zlp.mall.portal.modules.pms.service.HomeService;
import com.zlp.mall.portal.modules.pms.service.ProductCategoryService;
import com.zlp.mall.portal.modules.pms.service.ProductService;
import com.zlp.mall.portal.modules.sms.model.HomeAdvertise;
import com.zlp.mall.portal.modules.sms.model.HomeCategory;
import com.zlp.mall.portal.modules.sms.service.HomeAdvertiseService;
import com.zlp.mall.portal.modules.sms.service.HomeCategoryService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 产品分类 服务实现类
 * </p>
 *
 * @author ZouLiPing
 * @since 2021-10-13
 */
@Service
@RequiredArgsConstructor
@Slf4j(topic = "HomeServiceImpl")
public class HomeServiceImpl implements HomeService {

    private final ProductCategoryService productCategoryService;
    private final HomeAdvertiseService homeAdvertiseService;
    private final HomeCategoryService homeCategoryService;
    private final ProductService productService;

    @Override
    public HomeMenuBannerResp getMenusBanner() {
        log.info("getMenusBanner");
        HomeMenuBannerResp homeMenuBannerResp = new HomeMenuBannerResp();
        List<HomeResp> homeRespList = productCategoryService.getMenus();
        homeMenuBannerResp.setMenuRespList(homeRespList);
        List<HomeAdvertiseResp> advertiseList = homeAdvertiseService.getBannerList();
        homeMenuBannerResp.setAdvertiseList(advertiseList);
        return homeMenuBannerResp;
    }

    @Override
    public List<HomeGoodsSaleResp> goodsSale() {

        List<HomeGoodsSaleResp> homeGoodsSaleResps;
        LambdaQueryWrapper<HomeCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HomeCategory::getType,0);
        queryWrapper.eq(HomeCategory::getStatus,1);
        queryWrapper.orderByAsc(HomeCategory::getId);
        List<HomeCategory> homeCategoryList = homeCategoryService.list(queryWrapper);
        homeGoodsSaleResps = BeanToUtils.entityToList(homeCategoryList,HomeGoodsSaleResp.class);
        if (CollectionUtil.isNotEmpty(homeGoodsSaleResps)) {
            for (HomeGoodsSaleResp homeGoodsSaleResp : homeGoodsSaleResps) {
                List<Product> productList = productService.getByCategoryId(homeGoodsSaleResp.getCategoryId());
                homeGoodsSaleResp.setProductList(BeanToUtils.entityToList(productList,ProductResp.class));
            }
        }
        return homeGoodsSaleResps;
    }

}
